<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Java,Colletcion Framework," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="说明：Java Colletcion Framework 集合框架。">
<meta name="keywords" content="Java,Colletcion Framework">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 集合框架">
<meta property="og:url" content="http://bebetter.site/2017/09/01/Language/Java-advanced/Java 集合框架/index.html">
<meta property="og:site_name" content="gatewayzy">
<meta property="og:description" content="说明：Java Colletcion Framework 集合框架。">
<meta property="og:image" content="http://zcy.ckcest.cn/cdn/zy/20170907-Image-1.jpg">
<meta property="og:updated_time" content="2017-09-01T05:58:10.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 集合框架">
<meta name="twitter:description" content="说明：Java Colletcion Framework 集合框架。">
<meta name="twitter:image" content="http://zcy.ckcest.cn/cdn/zy/20170907-Image-1.jpg">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"right","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://bebetter.site/2017/09/01/Language/Java-advanced/Java 集合框架/"/>

  <title> Java 集合框架 | gatewayzy </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">gatewayzy</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">blog of gatewayzy</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Java 集合框架
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2017-09-01T13:58:10+08:00" content="2017-09-01">
              2017-09-01
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Language/" itemprop="url" rel="index">
                    <span itemprop="name">Language</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>说明：</strong>Java Colletcion Framework 集合框架。<br><a id="more"></a></p>
<p>参考文章：</p>
<h2 id="Colletcion-Framework-集合框架"><a href="#Colletcion-Framework-集合框架" class="headerlink" title="Colletcion Framework 集合框架"></a>Colletcion Framework 集合框架</h2><ul>
<li>Java 2 之前，Java 为对象组的存储和操作提供了特别的类比如 字典Dictionary，向量Vector，堆栈Stack和属性Properties。缺乏明确主题和分类。</li>
<li>后来提出了集合框架。集合框架中由接口、实现类、算法接口组成，构成完整的分类体系。</li>
</ul>
<p><img src="http://zcy.ckcest.cn/cdn/zy/20170907-Image-1.jpg" alt="image"></p>
<h3 id="集合接口"><a href="#集合接口" class="headerlink" title="集合接口"></a>集合接口</h3><table>
<thead>
<tr>
<th>接口名称</th>
<th>接口描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>Collection</td>
<td>这让你可以使用对象组；它是集合层次阶段的顶端</td>
</tr>
<tr>
<td>List</td>
<td>它继承了 Collection 并且 List 的一个实例存储了元素的一个有序集合</td>
</tr>
<tr>
<td>Set</td>
<td>它继承了 Collection 来处理集，它必须含有特殊的元素</td>
</tr>
<tr>
<td>SortedSet</td>
<td>它继承了 Set 来处理 排序的 set</td>
</tr>
<tr>
<td>Map</td>
<td>它将独特的键和值匹配</td>
</tr>
<tr>
<td>Map Entry</td>
<td>这描述了映射中的一个元素(一个键值对)。它是 Map 的一个内部类。</td>
</tr>
<tr>
<td>SortedMap</td>
<td>它继承了 Map 因此键按升序保持</td>
</tr>
<tr>
<td>Enumeration-&gt;Iterator</td>
<td>它是旧有的接口并定义了你可以在对象的集合中列举(一次获得一个)元素的方法。这个旧有的接口被迭代器Iterator取代了。</td>
</tr>
</tbody>
</table>
<h3 id="集合实现类"><a href="#集合实现类" class="headerlink" title="集合实现类"></a>集合实现类</h3><table>
<thead>
<tr>
<th>实现类的名称</th>
<th>类的描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>AbstractCollection</td>
<td>实现大部分的 Collection 接口  </td>
</tr>
<tr>
<td>AbstractList</td>
<td>继承 AbstractCollection 并且实现大部分 List 接口</td>
</tr>
<tr>
<td>AbstractSequentialList</td>
<td>通过一个使用有序的而不是随机访问它的元素的集合继承 AbstractList</td>
</tr>
<tr>
<td>LinkedList</td>
<td>通过继承 AbstractSequentialList 实现一个链表</td>
</tr>
<tr>
<td>ArrayList</td>
<td>通过继承 AbstractList 实现一个动态数组</td>
</tr>
<tr>
<td>AbstractSet</td>
<td>继承 AbstractCollection 并实现大部分的 Set 接口</td>
</tr>
<tr>
<td>HashSet</td>
<td>用一个哈希表继承 AbstractSet</td>
</tr>
<tr>
<td>LinkedHashSet</td>
<td>继承 HashSet 来允许插入顺序迭代</td>
</tr>
<tr>
<td>TreeSet</td>
<td>实现在树中存储的一个集。继承 AbstractSet</td>
</tr>
<tr>
<td>AbstractMap</td>
<td>实现大部分的 Map 接口</td>
</tr>
<tr>
<td>HashMap</td>
<td>用一个哈希表继承 AbstractMap</td>
</tr>
<tr>
<td>TreeMap</td>
<td>用一棵树继承 AbstractMap</td>
</tr>
<tr>
<td>WeakHashMap</td>
<td>用一个使用弱键的哈希表来继承 AbstractMap</td>
</tr>
<tr>
<td>LinkedHashMap</td>
<td>继承 AbstractMap 来允许插入顺序迭代</td>
</tr>
<tr>
<td>IdentityHashMap</td>
<td>继承 AbstractMap 类并且当比较文档时平等使用参考</td>
</tr>
</tbody>
</table>
<h3 id="旧有的集合类"><a href="#旧有的集合类" class="headerlink" title="旧有的集合类"></a>旧有的集合类</h3><table>
<thead>
<tr>
<th>类的名称</th>
<th>类的描述</th>
</tr>
</thead>
<tbody>
<tr>
<td> Vector</td>
<td>这实现一个动态数组。它和 ArrayList 类似，但也有一些不同。</td>
</tr>
<tr>
<td> Stack</td>
<td>Stack 是 Vector 的实现标准的后进先出栈的子类</td>
</tr>
<tr>
<td> Dictionary</td>
<td>是一个抽象的代表一个键值对存储库的类并且操作起来非常像 Map</td>
</tr>
<tr>
<td> Hashtable</td>
<td>Hashtable 是初始的 java.util 的一部分并且是 Dictionary 的具体实现</td>
</tr>
<tr>
<td> Properties</td>
<td>Properties 是 Hashtable 的一个子类。它被用来保持键是一个字符串并且值也是一个字符串的值的列表</td>
</tr>
<tr>
<td> BitSet</td>
<td>一个 BitSet 类创建一个特殊的保持 bit 数值的数组类型。这个数组的大小能根据需要增长</td>
</tr>
</tbody>
</table>
<h3 id="集合的算法"><a href="#集合的算法" class="headerlink" title="集合的算法"></a>集合的算法</h3><table>
<thead>
<tr>
<th>算法</th>
<th>类的描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>Iterable 接口</td>
<td>一个Collection可以用迭代器，是Collection接口的父类。</td>
</tr>
<tr>
<td>Iterator 接口</td>
<td>一个Collection的迭代器，用于获得或者删除元素。其子接口类ListIterator接口，允许双向遍历和修改。</td>
</tr>
<tr>
<td>Comparator 接口</td>
<td>比较器，compare、equals、reversed等方法 jdk1.8</td>
</tr>
<tr>
<td>Comparable 接口</td>
<td>元素可比较，需要实现compareTo方法</td>
</tr>
</tbody>
</table>
<h3 id="补充说明"><a href="#补充说明" class="headerlink" title="补充说明"></a>补充说明</h3><ul>
<li>很多抽象类，如AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList 和 AbstractMap 类提供了核心集合接口的实现，减少代码重复。</li>
<li>Collections类和Arrays类实现了大量static的方法，提供常用的集合操作。</li>
<li>还有其他一些接口和实现类并没有提到。</li>
<li>很多接口和类之间不是通过继承实现关系传递，而是通过程序组合起来的。比如Comparator和Comparable必然是联系的，但是不是继承关系。</li>
</ul>
<h3 id="HashSet如何保证元素不重复"><a href="#HashSet如何保证元素不重复" class="headerlink" title="HashSet如何保证元素不重复"></a>HashSet如何保证元素不重复</h3><p>在HashSet中，基本的操作都是有HashMap底层实现的，因为HashSet底层是用HashMap存储数据的。当向HashSet中添加元素的时候，首先计算元素的hashcode值，然后用这个（元素的hashcode）%（HashMap集合的大小）+1计算出这个元素的存储位置，如果这个位置位空，就将元素添加进去；如果不为空，则用equals方法比较元素是否相等，相等就不添加，否则找一个空位添加。</p>
<p>java中，两个对象的hashcode相等并不一定满足他们equals，但是满足equals必须使hashcode相等。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag">#Java</a>
          
            <a href="/tags/Colletcion-Framework/" rel="tag">#Colletcion Framework</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/01/Language/Java-jvm/Java 深入理解Java虚拟机/" rel="next" title="Java 深入理解Java虚拟机">
                <i class="fa fa-chevron-left"></i> Java 深入理解Java虚拟机
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/01/Language/Java-advanced/Generic 泛型/" rel="prev" title="Generic 泛型">
                Generic 泛型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    


  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/statics/images/avatar.png"
               alt="gatewayzy" />
          <p class="site-author-name" itemprop="name">gatewayzy</p>
          <p class="site-description motion-element" itemprop="description">blog website with hexo and github pages</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="https://github.com/gatewayzy" title="Github-gatewayzy" target="_blank">Github-gatewayzy</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://google.com/" title="Goolge" target="_blank">Goolge</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://wiki.jikexueyuan.com/" title="Wiki-jike" target="_blank">Wiki-jike</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Colletcion-Framework-集合框架"><span class="nav-number">1.</span> <span class="nav-text">Colletcion Framework 集合框架</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#集合接口"><span class="nav-number">1.1.</span> <span class="nav-text">集合接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#集合实现类"><span class="nav-number">1.2.</span> <span class="nav-text">集合实现类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#旧有的集合类"><span class="nav-number">1.3.</span> <span class="nav-text">旧有的集合类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#集合的算法"><span class="nav-number">1.4.</span> <span class="nav-text">集合的算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#补充说明"><span class="nav-number">1.5.</span> <span class="nav-text">补充说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HashSet如何保证元素不重复"><span class="nav-number">1.6.</span> <span class="nav-text">HashSet如何保证元素不重复</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">gatewayzy</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  
  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
  </script>

  <script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for (i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  </script>
  <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>



  

  
<script type="text/javascript" async src="//push.zhanzhang.baidu.com/push.js">
</script>


</body>
</html>
